package com.ruge.user.service.impl;

import com.ruge.core.response.Response;
import com.ruge.core.tool.RedisKeyTool;
import com.ruge.user.domain.request.BlobFollowRequest;
import com.ruge.user.error.UserError;
import com.ruge.user.service.BlobFollowService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ruge.wu
 * @since 2021/12/28 10:24
 */
@Service
public class BlobFollowServiceImpl implements BlobFollowService {

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 关注（关注接口）
     *
     * @param request {@link BlobFollowRequest}
     * @return {@link Response}
     */
    @Override
    public Response follow(BlobFollowRequest request) {
        Long aid = request.getAid();
        Long friendId = request.getFriendId();
        // 我的关注
        String followKey = RedisKeyTool.buildFollowKey(friendId);
        // 我的粉丝
        String fansKey = RedisKeyTool.buildFansKey(aid);

        Boolean member = redisTemplate.opsForSet().isMember(followKey, friendId);
        // 没有关注信息 关注
        if (!member && request.getFlag()) {
            redisTemplate.opsForSet().add(followKey, aid);
            redisTemplate.opsForSet().add(fansKey, friendId);
            // 没有关注信息 取关
        } else if (!member && !request.getFlag()) {
            return Response.create(UserError.NO_FOLLOW_RECORD);
            // 有关注信息 关注
        } else if (member && request.getFlag()) {
            return Response.create(UserError.NO_NEED_REPEAT_FOLLOW);
            // 有关注信息 取关
        } else if (member && !request.getFlag()) {
            redisTemplate.opsForSet().remove(followKey, aid);
            redisTemplate.opsForSet().remove(fansKey, friendId);
        }
        return Response.create();
    }

    /**
     * 查询我的关注（查看我的关注列表）
     * rugeSet -> {zhangsan,lisi,wangwu}
     * xuruiSet->{zhangsan,lisi,zhaoliu}
     *
     * @param aid aid
     * @return {@link Response}
     */
    @Override
    public Response getMyFollow(Long aid) {
        String blogKey = RedisKeyTool.buildFollowKey(aid);
        Set members = redisTemplate.opsForSet().members(blogKey);
        return Response.create(members);
    }

    /**
     * 查询我的粉丝列表
     *
     * @param aid aid
     * @return {@link Response}
     */
    @Override
    public Response getMyFans(Long aid) {
        String key = RedisKeyTool.buildFansKey(aid);
        Set members = redisTemplate.opsForSet().members(key);
        return Response.create(members);
    }

    /**
     * 共同关注 （返回我和博主的共同关注
     * sinter rugeSet xuruiSet -> {zhangsan,lisi}
     *
     * @param request {@link BlobFollowRequest}
     * @return {@link Response}
     */
    @Override
    public Response getCommonFollow(BlobFollowRequest request) {
        Long aid = request.getAid();
        Long friendId = request.getFriendId();
        String blogKey = RedisKeyTool.buildFollowKey(aid);
        String friendKey = RedisKeyTool.buildFollowKey(friendId);
        // 计算交集
        Set intersect = redisTemplate.opsForSet().intersect(blogKey, friendKey);
        return Response.create(intersect);
    }

    /**
     * 我关注的人也关注他
     * sismember zhagnsanSet xurui
     * sismember lisiSet xurui
     * sismember wangwuSet xurui
     *
     * @param request {@link BlobFollowRequest}
     * @return {@link Response}
     */
    @Override
    public Response getMyFollowTransFollow(BlobFollowRequest request) {
        Long aid = request.getAid();
        Long friendId = request.getFriendId();
        String blogKey = RedisKeyTool.buildFollowKey(aid);
        Set<Long> members = redisTemplate.opsForSet().members(blogKey);
        List<Long> aids = new ArrayList<>();
        for (Long member : members) {
            Boolean member1 = redisTemplate.opsForSet().isMember(RedisKeyTool.buildFollowKey(member), friendId);
            if (member1) {
                aids.add(member);
            }
        }
        return Response.create(aids);
    }

    /**
     * 我可能认识的人  @TODO
     * sdiff rugeSet xuruiSet
     *
     * @param aid aid
     * @return {@link Response}
     */
    @Override
    public Response getMyMayKnowPerson(Long aid) {
        String blogKey = RedisKeyTool.buildFollowKey(aid);
//        redisTemplate.opsForSet().difference(blogKey, friendId);
        return Response.create();
    }
}
